#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<int> path;
    bool check[8];
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        set<vector<int>> hash;
        int len = nums.size();
        path.resize(len);

        dfs(nums, 0, hash);
        vector<vector<int>> ret;
        for (auto& e : hash) ret.push_back(e);

        return ret;
    }

    void dfs(vector<int>& nums, int pos, set<vector<int>>& hash)
    {
        if (pos == nums.size())
        {
            hash.insert(path);
            return;
        }

        for (int i = 0; i < nums.size(); ++i)
        {
            if (check[i] == 0)
            {
                check[i] = 1;
                path[pos] = nums[i];
                dfs(nums, pos + 1, hash);
                check[i] = 0;
            }
        }
    }
};